//package com.cty.search.listener;
//import com.cty.doc.ProductDoc;
//import com.cty.pojo.Product;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.elasticsearch.action.delete.DeleteRequest;
//import org.elasticsearch.action.index.IndexRequest;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.common.xcontent.XContentType;
//import org.springframework.amqp.rabbit.annotation.Exchange;
//import org.springframework.amqp.rabbit.annotation.Queue;
//import org.springframework.amqp.rabbit.annotation.QueueBinding;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
////监听消息通知
//@Component
//public class RabbitMQListener {
//
//    @Autowired
//    private RestHighLevelClient client;
//
//    /**
//     * 监听并且插入数据的方法
//     */
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "insert.queue"),
//            exchange = @Exchange("topic.ex"),
//            key = "product.insert"
//    ))
//    public void insert(Product product) throws IOException {
//
//        IndexRequest indexRequest = new IndexRequest("product")
//                .id(product.getProductId().toString());
//
//        ProductDoc productDoc = new ProductDoc(product);
//
//        ObjectMapper objectMapper = new ObjectMapper();
//        //将Doc对象转成json字符串
//        String json  = objectMapper.writeValueAsString(productDoc);
//
//        indexRequest.source(json, XContentType.JSON);
//
//        //插入核心方法
//        client.index(indexRequest, RequestOptions.DEFAULT);
//
//    }
//
//    /**
//     * 删除数据
//     * @param productId
//     */
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "remove.queue"),
//            exchange = @Exchange("topic.ex"),
//            key = "product.remove"
//    ))
//    public void remove(Integer productId) throws IOException {
//
//        DeleteRequest request = new DeleteRequest("product")
//                .id(productId.toString());
//
//        client.delete(request,RequestOptions.DEFAULT);
//    }
//}
